<template>
  <div v-if="modelValue" class="modal-overlay active" @click="handleOverlayClick">
    <div class="modal-dialog">
      <div class="modal-header">
        <div class="modal-title">添加摄像头</div>
        <button class="modal-close" @click="close">×</button>
      </div>
      <div class="modal-body">
        <!-- 输入配置 -->
        <div class="modal-section">
          <div class="modal-section-title">输入配置</div>
          <div class="form-group">
            <label>输入源名称</label>
            <input type="text" v-model="config.name" placeholder="摄像头-1">
          </div>
          <div class="form-group">
            <label>视频设置</label>
            <select v-model="config.quality">
              <option value="1080p">1080p</option>
              <option value="720p">720p</option>
              <option value="480p">480p</option>
            </select>
          </div>
          <div class="form-group">
            <label>填充模式</label>
            <select v-model="config.fillMode">
              <option value="fit">适应（保持比例）</option>
              <option value="fill">填充（可能变形）</option>
              <option value="cover">覆盖（裁剪边缘）</option>
            </select>
            <small>控制视频如何填充区域</small>
          </div>
        </div>

        <!-- 布局配置 -->
        <div class="modal-section">
          <div class="modal-section-title">布局配置</div>
          <div class="form-row" style="margin-bottom: 15px;">
            <div class="form-group" style="margin-bottom: 0;">
              <label>画面宽</label>
              <input type="number" v-model.number="config.width" min="50" :max="canvasWidth">
              <small>最大: {{ canvasWidth }}</small>
            </div>
            <div class="form-group" style="margin-bottom: 0;">
              <label>画面高</label>
              <input type="number" v-model.number="config.height" min="50" :max="canvasHeight">
              <small>最大: {{ canvasHeight }}</small>
            </div>
          </div>
          <div class="form-row">
            <div class="form-group" style="margin-bottom: 0;">
              <label>X轴坐标</label>
              <input type="number" v-model.number="config.x" min="0" :max="canvasWidth">
              <small>范围: 0-{{ canvasWidth }}</small>
            </div>
            <div class="form-group" style="margin-bottom: 0;">
              <label>Y轴坐标</label>
              <input type="number" v-model.number="config.y" min="0" :max="canvasHeight">
              <small>范围: 0-{{ canvasHeight }}</small>
            </div>
          </div>
          <div class="preview-box" style="margin-top: 15px;">
            <div style="color: #f59e0b; font-weight: bold;">⚠️ 边界限制</div>
            <div style="margin-top: 8px;">X + 宽度 ≤ 混流宽度</div>
            <div>Y + 高度 ≤ 混流高度</div>
            <div style="margin-top: 8px; font-size: 11px; opacity: 0.8;">超出部分将自动修正</div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button class="btn btn-secondary" @click="close">取消</button>
        <button class="btn btn-primary" @click="confirm">确定</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  canvasWidth: {
    type: Number,
    default: 1280
  },
  canvasHeight: {
    type: Number,
    default: 720
  },
  streamCounter: {
    type: Number,
    default: 0
  }
})

const emit = defineEmits(['update:modelValue', 'confirm'])

const config = ref({
  name: '',
  quality: '720p',
  fillMode: 'fit',
  width: 320,
  height: 180,
  x: 50,
  y: 50
})

watch(() => props.modelValue, (newVal) => {
  if (newVal) {
    // 重置配置
    config.value = {
      name: `摄像头-${props.streamCounter + 1}`,
      quality: '720p',
      fillMode: 'fit',
      width: 320,
      height: 180,
      x: 50,
      y: 50
    }
  }
})

const close = () => {
  emit('update:modelValue', false)
}

const handleOverlayClick = (e) => {
  if (e.target.classList.contains('modal-overlay')) {
    close()
  }
}

const confirm = () => {
  // 边界检查和自动修正
  config.value.width = Math.max(50, Math.min(props.canvasWidth, config.value.width))
  config.value.height = Math.max(50, Math.min(props.canvasHeight, config.value.height))
  config.value.x = Math.max(0, Math.min(props.canvasWidth - config.value.width, config.value.x))
  config.value.y = Math.max(0, Math.min(props.canvasHeight - config.value.height, config.value.y))
  
  emit('confirm', { ...config.value })
  close()
}
</script>

<style scoped>
.modal-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}

.modal-overlay.active {
  display: flex;
}

.modal-dialog {
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  animation: modalSlideIn 0.3s ease-out;
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: translateY(-50px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.modal-header {
  padding: 20px 25px;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  color: #999;
  cursor: pointer;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.3s;
}

.modal-close:hover {
  background: #f0f0f0;
  color: #333;
}

.modal-body {
  padding: 25px;
}

.modal-section {
  margin-bottom: 25px;
}

.modal-section:last-child {
  margin-bottom: 0;
}

.modal-section-title {
  font-size: 15px;
  font-weight: bold;
  color: #667eea;
  margin-bottom: 15px;
  padding-bottom: 8px;
  border-bottom: 2px solid #e0e0e0;
}

.modal-footer {
  padding: 15px 25px;
  border-top: 1px solid #e0e0e0;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  font-weight: 600;
  color: #555;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
  transition: border-color 0.3s;
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: #667eea;
}

.form-group small {
  display: block;
  margin-top: 4px;
  font-size: 12px;
  color: #888;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.preview-box {
  background: #f0f4f8;
  border: 2px dashed #cbd5e0;
  border-radius: 8px;
  padding: 20px;
  text-align: center;
  color: #718096;
  font-size: 13px;
  min-height: 120px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.btn {
  padding: 12px 20px;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.btn-secondary {
  background: #6b7280;
  color: white;
}
</style>

